package com.kermel.common.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateTimeUtils {
    public static final long MILLISECONDS_PER_SECOND = 1000;
    
    public static final long SECONDS_PER_MINUTE = 60;
    public static final long MILLISECONDS_PER_MINUTE = SECONDS_PER_MINUTE * MILLISECONDS_PER_SECOND;
    
    public static final long MINUTES_PER_HOUR = 60;
    public static final long SECONDS_PER_HOUR = MINUTES_PER_HOUR * SECONDS_PER_MINUTE;
    public static final long MILLISECONDS_PER_HOUR = SECONDS_PER_HOUR * MILLISECONDS_PER_SECOND;
    
    public static final long HOURS_PER_DAY = 24;
    public static final long MINUTES_PER_DAY = MINUTES_PER_HOUR * HOURS_PER_DAY;
    public static final long SECONDS_PER_DAY = MINUTES_PER_DAY * SECONDS_PER_MINUTE;
    public static final long MILLISECONDS_PER_DAY = SECONDS_PER_DAY * MILLISECONDS_PER_SECOND;
    
    public static final String HOURS_MINUTES_DATE_FORMAT_PATTERN = "HH:mm";
    
    public static long rightNowInMilliseconds() {
        return Calendar.getInstance().getTimeInMillis();
    }
    
    public static String rightNowToDateStringFull() {
        long rightNowMS = rightNowInMilliseconds();
        return timeInMillisecondsToDateStringFull(rightNowMS);
    }
    
    public static String timeInMillisecondsToDateStringFull(long timeInMilliseconds) {
        SimpleDateFormat formatter = new SimpleDateFormat("EEEE, MMMM dd, yyyy HH:mm:ss.SSS");
        
        Date date = timeInMillisecondsToDate(timeInMilliseconds);
        
        formatter.format(date);
        
        return formatter.format(date);
    }
    
    public static Date timeInMillisecondsToDate(long timeInMilliseconds) {
        Date date = new Date(timeInMilliseconds);
        return date;
    }
    
    public static long adjustDateTime(long dateTimeInMilliseconds, int millisecondsToAddOrSubtract) {
        long adjustedTimeInMilliseconds = dateTimeInMilliseconds;

        Calendar dateTime = Calendar.getInstance();
        dateTime.setTimeInMillis(dateTimeInMilliseconds);
        dateTime.add(Calendar.MILLISECOND, millisecondsToAddOrSubtract);
        
        adjustedTimeInMilliseconds = dateTime.getTimeInMillis();
        
        return adjustedTimeInMilliseconds;
    }
    
    public static long secondsToMilliseconds(long numberOfSeconds) {
        return numberOfSeconds * MILLISECONDS_PER_SECOND;
    }
    
    public static long minutesToSeconds(long numberOfMinutes) {
        return numberOfMinutes * SECONDS_PER_MINUTE;
    }
    
    public static long minutesToMilliseconds(long numberOfMinutes) {
        return numberOfMinutes * MILLISECONDS_PER_MINUTE;
    }
    
    public static long hoursToMinutes(long numberOfHours) {
        return numberOfHours * MINUTES_PER_HOUR;
    }
    
    public static long hoursToSeconds(long numberOfHours) {
        return numberOfHours * SECONDS_PER_HOUR;
    }
    
    public static long hoursToMilliseconds(long numberOfHours) {
        return numberOfHours * MILLISECONDS_PER_HOUR;
    }
    
    public static long daysToHours(long numberOfDays) {
        return numberOfDays * HOURS_PER_DAY;
    }
    
    public static long daysToMinutes(long numberOfDays) {
        return numberOfDays * MINUTES_PER_DAY;
    }
    
    public static long daysToSeconds(long numberOfDays) {
        return numberOfDays * SECONDS_PER_DAY;
    }
    
    public static long daysToMilliseconds(long numberOfDays) {
        return numberOfDays * MILLISECONDS_PER_DAY;
    }
    
    public static Calendar createCalendar(long timeInMilliseconds) {
        Calendar dateTime = Calendar.getInstance();
        dateTime.setTimeInMillis(timeInMilliseconds);
        return dateTime;
    }

}
